package com.qcu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcu.dto.Review;
import com.qcu.dto.chart.UserGoodCount;
import com.qcu.entity.Post;
import com.qcu.entity.PostFailLog;
import com.qcu.entity.StaffAccount;
import com.qcu.mapper.PostMapper;
import com.qcu.service.PostFailLogService;
import com.qcu.service.PostService;
import com.qcu.service.StaffAccountService;
import com.qcu.util.Response;
import com.qcu.util.ResponseCode;
import com.qcu.util.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @author MyComputer
* @description 针对表【post】的数据库操作Service实现
* @createDate 2025-02-15 09:28:14
*/
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService{

    @Resource
    private PostMapper postMapper;
    @Resource
    private StaffAccountService staffAccountService;
    @Resource
    PostFailLogService postFailLogService;
    @Override
    public boolean savePost(String title, String content, String imgUrl) {
        // 查找账号中带有 ‘plain’ 的员工
        LambdaQueryWrapper<StaffAccount> wrapperStaff = new LambdaQueryWrapper<>();
        wrapperStaff.eq(StaffAccount::getType,4);
        List<StaffAccount> list = staffAccountService.list(wrapperStaff);
        // 随机获取员工id
        StaffAccount staffAccount = list.get((int) (Math.random() * list.size()));

        Post post = new Post();
        post.setAudit(staffAccount.getStaffId());
        post.setTitle(title);
        post.setContent(content);
        post.setPicture(imgUrl);
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        post.setUserId(id);
        post.setState(0);
        post.setGood(0);
        int insert = postMapper.insert(post);
        return insert > 0 ? true : false;
    }
    @Override
    public Response getAllPost(Integer page, Integer size) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        Page<Post> pageInfo = new Page<>(page,size);
        Page<Post> pageResult = postMapper.selectPage(pageInfo, new LambdaQueryWrapper<Post>().eq(Post::getAudit,id));
        return Response.success(pageResult);
    }
    @Override
    @Transactional
    public Response auditPost(Review review) {
        Post posts = postMapper.selectOne(new LambdaQueryWrapper<Post>().eq(Post::getId, review.getId()));
        PostFailLog postFailLog = new PostFailLog();
        BeanUtils.copyProperties(posts,postFailLog);
        LambdaUpdateWrapper<Post> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Post::getId,review.getId()).set(Post::getState,review.getState());
        int update = postMapper.update(null, wrapper);
        boolean save = false;
        if (review.getState() == 2){
            save = postFailLogService.save(postFailLog);
        }else {
            save = true;
        }
        return ( update > 0 && save ) ? Response.success() : Response.failure(ResponseCode.FAILURE);
    }

    @Override
    public List<UserGoodCount> getTopUserGoodCounts() {
        return postMapper.getTopUserGoodCounts();
    }
}




